package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mbc.rmt.entities.AddCodeDetail;
import com.mbc.common.dao.DAO;

public class AddCodeDetailDAOImpls extends DAO<AddCodeDetail> implements
		AddCodeDetailDAO {

	public AddCodeDetailDAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "AddCodeDetail";
	}

	@Override
	public void create(AddCodeDetail entity) throws SQLException {
		query = "insert into "
				+ table_name
				+ " ( AddCodeDetailId, AddCodeId, Receiver, NRC, Address) values( ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getAddCodeDetailId());
		pstmt.setLong(i++, entity.getAddCodeId());
		pstmt.setString(i++, entity.getReceiver());
		pstmt.setString(i++, entity.getNRC());
		pstmt.setString(i++, entity.getAddress());

		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(AddCodeDetail entity) throws SQLException {
		query = "update " + table_name
				+ " set  AddCodeId = ?, Receiver = ?, NRC = ?, Address = ?"
				+ " where AddCodeDetailId = " + entity.getAddCodeDetailId();
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getAddCodeId());
		pstmt.setString(i++, entity.getReceiver());
		pstmt.setString(i++, entity.getNRC());
		pstmt.setString(i++, entity.getAddress());

		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery() + " where AddCodeDetailId = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public AddCodeDetail find(long key) throws SQLException {
		AddCodeDetail l_AddCodeDetail = null;
		query = getSelectQuery() + " where AddCodeDetailId = " + key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_AddCodeDetail = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_AddCodeDetail;
	}

	@Override
	public ArrayList<AddCodeDetail> findAll() throws SQLException {
		ArrayList<AddCodeDetail> l_AddCodeDetailList = new ArrayList<AddCodeDetail>();
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_AddCodeDetailList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_AddCodeDetailList;
	}

	@Override
	protected AddCodeDetail readRecord(ResultSet rs) throws SQLException {
		AddCodeDetail l_entity = new AddCodeDetail();
		l_entity.setAddCodeDetailId(rs.getLong("AddCodeDetailId"));
		l_entity.setAddCodeId(rs.getLong("AddCodeId"));
		l_entity.setReceiver(rs.getString("Receiver"));
		l_entity.setNRC(rs.getString("NRC"));
		l_entity.setAddress(rs.getString("Address"));

		return l_entity;
	}

	@Override
	public void deleteByAddCodeId(long addCodeId) throws SQLException {
		query = getDeleteQuery() + " where AddCodeId = " + addCodeId;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public List<AddCodeDetail> findByAddCodeId(long addCodeId)
			throws SQLException {
		ArrayList<AddCodeDetail> l_AddCodeDetailList = new ArrayList<AddCodeDetail>();
		query = getSelectQuery() + " where AddCodeId = " + addCodeId;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_AddCodeDetailList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_AddCodeDetailList;
	}

	@Override
	public AddCodeDetail findLastByAddCodeId(long addCodeId)
			throws SQLException {
		AddCodeDetail l_AddCodeDetail = null;
		query = "select top(1)* from " + table_name + " where AddCodeId = "
				+ addCodeId + " order by AddCodeDetailId desc";
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_AddCodeDetail = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_AddCodeDetail;
	}
    //MBC Begin
	@Override
	public ArrayList<AddCodeDetail> getInfoList(long key) throws SQLException {
		ArrayList<AddCodeDetail> l_result = new ArrayList<AddCodeDetail>();
		query = " select * from " + table_name + " where AddCodeId = " + key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		
		while (rs.next()) {
			AddCodeDetail l_detail = readRecord(rs);
			l_result.add(l_detail);
		}
		rs.close();
		pstmt.close();
		return l_result;
	}
}
